package org.example.controller;


import org.example.entity.DormitoryAdmin;
import org.example.form.RuleForm;
import org.example.form.SearchForm;
import org.example.service.DormitoryAdminService;
import org.example.vo.PageVO;
import org.example.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-10-31
 */
@RestController
@RequestMapping("/dormitoryAdmin")
public class DormitoryAdminController {
    @Autowired
   private DormitoryAdminService dormitoryAdminService;
    @GetMapping("/login")
    public ResultVO login(RuleForm ruleForm){
       ResultVO resultVO = this.dormitoryAdminService.login(ruleForm);
        return resultVO;
    }
    @PostMapping("/save")
    public ResultVO save(@RequestBody DormitoryAdmin dormitoryAdmin) {//
        //除了get方法之外，其他方法都需要加上@RequestBody注解，表示请求体中的json数据将被解析为对象，并作为参数传入到方法中
        // 这里的@RequestBody注解表示请求体中的json数据将被解析为对象，并作为参数传入到方法中
        boolean flag = this.dormitoryAdminService.save(dormitoryAdmin);  //save方法是接口自带的方法，不需要实现，只需要调用即可
        if (flag) {

            return ResultVO.success(null);
        } else {

            return ResultVO.error("保存失败");
        }
    }
        @GetMapping("/list/{page}/{size}")
        public ResultVO list(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
            PageVO pageVO = this.dormitoryAdminService.list(page, size);
            return ResultVO.success(pageVO);
        }
        @GetMapping("/search")
        public ResultVO search(SearchForm searchForm){
            PageVO pageVO = this.dormitoryAdminService.search(searchForm);
            return ResultVO.success(pageVO);
        }
        @GetMapping("/findById/{id}")
        public ResultVO findById(@PathVariable("id") Integer id){
            DormitoryAdmin dormitoryAdmin = this.dormitoryAdminService.getById(id);
            return ResultVO.success(dormitoryAdmin);
        }
        @PutMapping("/update")
        public ResultVO update(@RequestBody DormitoryAdmin dormitoryAdmin){
            boolean flag = this.dormitoryAdminService.updateById(dormitoryAdmin);
            if (flag) {
                return ResultVO.success(null);
            } else {
                return ResultVO.error("更新失败");
            }
        }
        @DeleteMapping("/deleteById/{id}")
        public ResultVO deleteById(@PathVariable("id") Integer id){
            boolean flag = this.dormitoryAdminService.removeById(id);
            if (flag) {
                return ResultVO.success(null);
            }    else {    return ResultVO.error("删除失败");    }
        }

        //重载一个list方法
        @GetMapping("/list")
        public ResultVO list(){
            List<DormitoryAdmin> dormitoryAdmins = this.dormitoryAdminService.list();
            return ResultVO.success(dormitoryAdmins);
        }


}

